package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.survey.SurveyQuestionnaire;
import com.ruoyi.system.domain.survey.SurveyApproval;
import com.ruoyi.system.service.ISurveyQuestionnaireService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问卷调查 Controller
 * 
 */
@RestController
@RequestMapping("/system/survey")
public class SurveyQuestionnaireController extends BaseController
{
    @Autowired
    private ISurveyQuestionnaireService surveyQuestionnaireService;

    /**
     * 查询问卷列表
     */
    @PreAuthorize("@ss.hasPermi('system:survey:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurveyQuestionnaire surveyQuestionnaire)
    {
        startPage();
        List<SurveyQuestionnaire> list = null;
        //除了管理员以外  只能查看自己的问卷
        if(!SysUser.isAdmin(getUserId())){
            surveyQuestionnaire.setCreateBy(getUsername());
        }
        list = surveyQuestionnaireService.selectSurveyQuestionnaireList(surveyQuestionnaire);
        return getDataTable(list);
    }
    
    /**
     * 查询待审批问卷列表
     */
    @PreAuthorize("@ss.hasPermi('system:survey:approval')")
    @GetMapping("/approval/list")
    public TableDataInfo approvalList(SurveyQuestionnaire surveyQuestionnaire)
    {
        startPage();
        List<SurveyQuestionnaire> list = surveyQuestionnaireService.selectSurveyApprovalList(surveyQuestionnaire);
        return getDataTable(list);
    }

    /**
     * 导出问卷列表
     */
    @PreAuthorize("@ss.hasPermi('system:survey:export')")
    @Log(title = "问卷调查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SurveyQuestionnaire surveyQuestionnaire)
    {
        List<SurveyQuestionnaire> list = surveyQuestionnaireService.selectSurveyQuestionnaireList(surveyQuestionnaire);
        ExcelUtil<SurveyQuestionnaire> util = new ExcelUtil<SurveyQuestionnaire>(SurveyQuestionnaire.class);
        return util.exportExcel(list, "问卷数据");
    }

    /**
     * 获取问卷详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:survey:query')")
    @GetMapping(value = "/{surveyId}")
    public AjaxResult getInfo(@PathVariable("surveyId") Long surveyId)
    {
        return success(surveyQuestionnaireService.selectSurveyQuestionnaireById(surveyId));
    }

    /**
     * 新增问卷
     */
    @PreAuthorize("@ss.hasPermi('system:survey:add')")
    @Log(title = "问卷调查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurveyQuestionnaire surveyQuestionnaire)
    {
        return toAjax(surveyQuestionnaireService.insertSurveyQuestionnaire(surveyQuestionnaire));
    }

    /**
     * 修改问卷
     */
    @PreAuthorize("@ss.hasPermi('system:survey:edit')")
    @Log(title = "问卷调查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurveyQuestionnaire surveyQuestionnaire)
    {
        return toAjax(surveyQuestionnaireService.updateSurveyQuestionnaire(surveyQuestionnaire));
    }

    /**
     * 删除问卷
     */
    @PreAuthorize("@ss.hasPermi('system:survey:remove')")
    @Log(title = "问卷调查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{surveyIds}")
    public AjaxResult remove(@PathVariable Long[] surveyIds)
    {
        return toAjax(surveyQuestionnaireService.deleteSurveyQuestionnaireByIds(surveyIds));
    }
    
    /**
     * 发布问卷
     */
    @PreAuthorize("@ss.hasPermi('system:survey:publish')")
    @Log(title = "问卷调查", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{surveyId}")
    public AjaxResult publish(@PathVariable("surveyId") Long surveyId)
    {
        return toAjax(surveyQuestionnaireService.publishSurveyQuestionnaire(surveyId));
    }
    
    /**
     * 结束问卷
     */
    @PreAuthorize("@ss.hasPermi('system:survey:end')")
    @Log(title = "问卷调查", businessType = BusinessType.UPDATE)
    @PutMapping("/end/{surveyId}")
    public AjaxResult end(@PathVariable("surveyId") Long surveyId)
    {
        return toAjax(surveyQuestionnaireService.endSurveyQuestionnaire(surveyId));
    }
    
    /**
     * 提交审批
     */
    @PreAuthorize("@ss.hasPermi('system:survey:submit')")
    @Log(title = "问卷调查", businessType = BusinessType.UPDATE)
    @PutMapping("/submit/{surveyId}")
    public AjaxResult submit(@PathVariable("surveyId") Long surveyId)
    {
        return toAjax(surveyQuestionnaireService.submitSurveyApproval(surveyId));
    }
    
    /**
     * 审批问卷
     */
    @PreAuthorize("@ss.hasPermi('system:survey:approval')")
    @Log(title = "问卷调查", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public AjaxResult approve(@RequestBody SurveyApproval surveyApproval)
    {
        return toAjax(surveyQuestionnaireService.approveSurveyQuestionnaire(surveyApproval));
    }
} 